package  com.smartlbs.baseframe.utils; 

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 生成验证码工具类
 * 
 * 
 * 
 *
 */
public class VerificationCodeUtils {
	
	//定义图片的width
    private static final int WIDTH = 98;
    //定义图片的height
    private static final int HEIGHT = 33;
    //定义图片上显示验证码的个数
    private static final int CODE_COUNT = 4;
    
    private static final int XX = 22;
    //字体高度
    private static final int FONT_HEIGHT = 22;
    //
    private static final int CODEY = 25;

    private static final char[] ALL_RANDOM = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
            'K', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };

	
	/**
	 * 获取验证码信息
	 * 
	 * 
	 * 
	 * 
	 * @return
	 */
	public static Map<String, Object> getVerificationCode(){
        BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        Graphics gd = buffImg.getGraphics();
        Random random = new Random();

        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, WIDTH, HEIGHT);

        Font font = new Font("Fixedsys", Font.BOLD, FONT_HEIGHT);
        gd.setFont(font);

        gd.setColor(new Color(0,181,242));
        gd.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);

        // 随机产生40条干扰线，使图象中的认证码不易被其它程序探测到。
        gd.setColor(Color.BLACK);
        
        for (int i = 0; i < 20; i++) {
            int x = random.nextInt(WIDTH);
            int y = random.nextInt(HEIGHT);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
        }

        // randomCode用于保存随机产生的验证码，以便用户登录后进行验证。
        StringBuffer randomCode = new StringBuffer();
        int red = 0, green = 0, blue = 0;

        // 随机产生codeCount数字的验证码。
        for (int i = 0; i < CODE_COUNT; i++) {
            String code = String.valueOf(ALL_RANDOM[random.nextInt(30)]);
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);

            gd.drawString(code, (i) * XX+5, CODEY);

            randomCode.append(code);
        }
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        //
        resultMap.put("code", randomCode.toString().toLowerCase());
        resultMap.put("image", buffImg);
        
        return resultMap;
	}
}
